import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter_kit_plus/flutter_kit.dart';

import '../../commont/button.dart';
import '../../model/model.dart';
import '../../tool/assets.dart';

class GaidDbItem extends StatefulWidget {
  ChannelModel ch;
  double width;
  void Function(ChannelModel, String) onChange;
  void Function(ChannelModel, String) onChangeForSend;

  GaidDbItem({super.key, required this.ch, required this.width, required this.onChange, required this.onChangeForSend});
  @override
  State<GaidDbItem> createState() => _GaidDbItemState();
}

class _GaidDbItemState extends State<GaidDbItem> {
  Widget _img_item(bool isAdd) {
    return imgButton(
        enable: true,
        groundColor: Color_bg_view,
        img: Image.asset(isAdd ? assets.add : assets.sum, color: Colors.black),
        onClick: () {
          setState(() {
            widget.ch.db = isAdd ? widget.ch.db + 1 : widget.ch.db - 1;
            widget.ch.db = max(widget.ch.db, widget.ch.min_db);
            widget.ch.db = min(widget.ch.db, widget.ch.max_db);
          });
          widget.onChange(widget.ch, 'DB');
        });
  }

  void _select_ch() {
    setState(() {
      widget.ch.select = true;
    });
    widget.onChange(widget.ch, '');
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        ///通道
        AspectRatio(
            aspectRatio: 2 / GlobalConfig.scaleH,
            child: kTextButton(
                name: widget.ch.name,
                enable: true,
                fontSize: 14,
                textColor: widget.ch.select ? Colors.white : Colors.black,
                borderColor: widget.ch.select ? Color_theme : Colors.black,
                groundColor: widget.ch.select ? Color_theme : Color_bg_view,
                onClick: (p0) => _select_ch())),
        SizedBox(height: 5 * GlobalConfig.scaleH),

        ///分贝
        AspectRatio(
            aspectRatio: 2.5 / GlobalConfig.scaleH,
            child: kTextButton(
                name: '${widget.ch.db}' + 'dB',
                enable: true,
                fontSize: 13,
                textColor: widget.ch.select ? Colors.white : Colors.black,
                borderColor: widget.ch.select ? Color_theme : Colors.black,
                groundColor: widget.ch.select ? Color_theme : Color_bg_view,
                onClick: (p0) => _select_ch())),
        SizedBox(height: 5 * GlobalConfig.scaleH),

        ///增加分贝
        SizedBox(width: 18 * GlobalConfig.scaleH, height: 18 * GlobalConfig.scaleH, child: _img_item(true)),
        SizedBox(height: 2 * GlobalConfig.scaleH),

        ///滑动块
        Expanded(
            child: kSlider(
          enable: true,
          value: widget.ch.db.toDouble(),
          min: widget.ch.min_db.toDouble(),
          max: widget.ch.max_db.toDouble(),
          // enable: widget.ch.enable && widget.ch.select,
          thumbColor: widget.ch.select ? Color_thumb_slider : Color_disEnable,
          activeColor: widget.ch.select ? Color_thumb_slider : Color_disEnable,
          dirction: Axis.horizontal,
          onChanged: (p0) {
            setState(() {
              widget.ch.db = ((p0 * 100).toDouble() / 100.0).toInt();
            });
            widget.onChange(widget.ch, '');
          },
          onChangedForSend: (p0) => widget.onChangeForSend(widget.ch, 'DB'),
        )),

        ///减少分贝
        SizedBox(width: 18 * GlobalConfig.scaleH, height: 18 * GlobalConfig.scaleH, child: _img_item(false)),
        SizedBox(height: 5 * GlobalConfig.scaleH),

        ///静音
        AspectRatio(
            aspectRatio: 2.5 / GlobalConfig.scaleH,
            child: kTextButton(
              fontSize: 14,
              name: 'Mute',
              enable: true,
              textColor: widget.ch.mute ? Colors.white : Color_text,
              borderColor: widget.ch.mute ? Color_theme : Color_text,
              groundColor: widget.ch.mute ? Color_theme : Color_bg_view,
              onClick: (p0) {
                setState(() {
                  widget.ch.mute = !widget.ch.mute;
                });
                widget.onChange(widget.ch, 'DB');
              },
            )),

        SizedBox(height: 5 * GlobalConfig.scaleH),

        ///相位
        AspectRatio(
            aspectRatio: 2.5 / GlobalConfig.scaleH,
            child: kTextButton(
              fontSize: 14,
              name: widget.ch.phase ? '180°' : '0°',
              enable: widget.ch.select,
              textColor: widget.ch.select ? Color_text : Color_disEnable,
              borderColor: widget.ch.select ? Color_text : Color_disEnable,
              onClick: (p0) {
                setState(() {
                  widget.ch.phase = !widget.ch.phase;
                });
                widget.onChange(widget.ch, 'PHASE');
              },
            )),
      ],
    );
  }
}
